import {
	ref
} from 'vue';

class Common {
	throttleInt = 0;
	constructor() {

	}
	/**
	 * 提示
	 */
	showToast(title, icon = "none", time = 1500) {
		uni.showToast({
			title: title,
			icon: icon,
			duration: time
		})
	}

	/**
	 * 判断是否为空或者是否为false 如果为空则返回true 为空提示内容
	 * @param {Object} title
	 * @param {Object} data
	 */
	isFalse(data, title) {
		if (!data) {
			if (title) {
				this.showToast(title);
			}
			return true;
		}
		return false;
	}

	/**
	 * 节流防抖
	 * @param {Object} func
	 * @param {Object} timeOut
	 */
	throttle(func, timeOut) {
		clearTimeout(this.throttleInt);
		this.throttleInt = setTimeout(() => {
			func();
		}, timeOut);
	}

	/**
	 * bytes 单位转换
	 * @param {Object} bytes
	 */
	formatBytes(bytes) {
		if (bytes < 1024) {
			return bytes + " Bytes";
		} else if (bytes < 1048576) {
			return (bytes / 1024).toFixed(2) + " KB";
		} else if (bytes < 1073741824) {
			return (bytes / 1048576).toFixed(2) + " MB";
		} else if (bytes < 1099511627776) {
			return (bytes / 1073741824).toFixed(2) + " GB";
		} else {
			return (bytes / 1099511627776).toFixed(2) + " TB";
		}
	}

	/**
	 * uni modal弹窗
	 * @param {Object} options
	 */
	showModal(options) {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title: options.title,
				content: options.content,
				success: (res) => {
					options.success && options.success(res);
					resolve(res);
				},
				fail: (err) => {
					options.fail && options.fail(err);
					reject(err);
				}
			})
		})
	}

	/**
	 * 上传图片
	 * @param {Object} count 上传数量
	 * @param {Object} func 成功回调函数
	 */
	chooseImageUpload(count, func) {
		uni.chooseImage({
			count,
			success: (res) => {
				res.tempFilePaths.forEach(v => {
					uni.$req.upload('user/app/common/upload', v).then(ress => {
						func(ress);
					});
				})
			}
		});
	}

	/**
	 * 发送消息
	 * @param uni uni对象
	 * @param im im对象
	 * @param qnyUuid 七牛云id
	 * @param objContent 内容
	 */
	sendMessage(uni, im, qnyUuid, objContent, type = "text") {
		im.sysManage.sendRosterMessage({
			uid: qnyUuid,
			content: JSON.stringify(objContent),
			type
		});
		this.saveMessage(uni,im,qnyUuid,objContent);
	}
	
	/**
	 * 存储会话列表
	 * @param uni uni对象
	 * @param im im对象
	 * @param qnyUuid 七牛云id
	 * @param objContent 内容
	 */
	saveMessage(uni,im,qnyUuid,objContent) {
		if(!objContent.aimUserId) return ;
		let message = uni.getStorageSync("message");
		if (!message) {
			message = [];
		}
		let content = "";
		if (objContent.type === "LIVE") {
			content = "[直播]";
		} else if (objContent.type === "VIDEO") {
			content = "[视频]";
		} else if (objContent.type === "IMAGE") {
			content = "[图片]";
		} else if (objContent.type === "SHARE_VIDEO") {
			content = "[分享视频]";
		} else if (objContent.type === "TEXT") {
			content = objContent.content;
		}
		// 清除空数据
		let removeEmptyIndexs = [];
		message.forEach((v, i) => {
			if (v.sessionId === undefined) {
				removeEmptyIndexs.push(i);
			}
		});
		removeEmptyIndexs.forEach((item, index) => {
			message.splice(item - index, 1);
		});
		// 清除重复数据
		let removeOldIndexs = [];
		message.forEach((v, i) => {
			if(v.sessionId == qnyUuid) {
				removeOldIndexs.push(i);
			}
		});
		let maxObj = {
			userId: objContent.aimUserId,
			sessionId: qnyUuid,
			nickname: objContent.aimNickname,
			photo: objContent.aimPhoto,
			createTime: uni.$u.timeFormat(objContent.time, 'yyyy-mm-dd hh:MM:ss'),
			isNotRead: im.rosterManage.getUnreadCount(qnyUuid),
			content
		};
		removeOldIndexs.forEach((item, index) => {
			let oldTime = new Date(message[item - index].createTime.replace(/-/g, '/')).getTime();
			let newTime = new Date(maxObj.createTime.replace(/-/g, '/')).getTime();
			if(oldTime > newTime) {
				maxObj = message[item - index];
			}
			message.splice(item - index, 1);
		});
		if(qnyUuid === objContent.sessionId) return ;
		message.unshift(maxObj);
		uni.setStorageSync("message", message);
	}

	/**
	 * 获取通讯录列表
	 * @param addressSuccess 获取通信录成功回调
	 * @param success 获取联系人成功回调
	 * @param fail 失败回调
	 */
	addressBookList(addressSuccess,success,fail) {
		// 获取通讯录对象  
		plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, addressbook => {
			addressSuccess(addressbook);
			// 查找联系人  
			addressbook.find(["displayName", "phoneNumbers"], contacts => {
				success(contacts);
			}, () => {
				fail("获取联系人失败");
			}, {
				multiple: true
			});
		}, e => {
			fail("获取通讯录失败"+e.message);
		});

	}
}

export default Common;